package cn.cdeden.aiqa.service.impl;

import cn.cdeden.aiqa.domain.WikiRecord;
import cn.cdeden.aiqa.domain.bo.ChatRecordQueryParam;
import cn.cdeden.aiqa.domain.bo.WikiRecordBo;
import cn.cdeden.aiqa.domain.vo.WikiRecordVo;
import cn.cdeden.aiqa.mapper.WikiRecordMapper;
import cn.cdeden.aiqa.service.IWikiRecordService;
import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.StringUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.system.api.domain.SysUserOnline;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * AI提问记录Service业务层处理
 *
 * @author Nick
 * @date 2024-12-12
 */
@RequiredArgsConstructor
@Service
public class WikiRecordServiceImpl implements IWikiRecordService {

    private final WikiRecordMapper baseMapper;

    /**
     * 查询AI提问记录
     *
     * @param id 主键
     * @return AI提问记录
     */
    @Override
    public WikiRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询AI提问记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return AI提问记录分页列表
     */
    @Override
    public TableDataInfo<WikiRecordVo> queryPageList(WikiRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WikiRecord> lqw = buildQueryWrapper(bo);


        // 使用 Page 对象来进行分页查询
        Page<WikiRecordVo> result = baseMapper.selectVoPageWithAppInfo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的AI提问记录列表
     *
     * @param bo 查询条件
     * @return AI提问记录列表
     */
    @Override
    public List<WikiRecordVo> queryList(WikiRecordBo bo) {
        LambdaQueryWrapper<WikiRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WikiRecord> buildQueryWrapper(WikiRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WikiRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), WikiRecord::getTitle, bo.getTitle());
        lqw.eq(bo.getReCount() != null, WikiRecord::getReCount, bo.getReCount());
        lqw.eq(StringUtils.isNotBlank(bo.getRecord()), WikiRecord::getRecord, bo.getRecord());
        lqw.eq(bo.getAssistantId() != null, WikiRecord::getAssistantId, bo.getAssistantId());
        if(StringUtils.isNotBlank(bo.getNickName())) {
            lqw.apply("su.nick_name LIKE {0}", "%" + bo.getNickName() + "%");
        }
        if(bo.getUserId() != null) {
            lqw.apply("wr.user_id = {0}", bo.getUserId());
        }
        // 添加关联查询
        lqw.eq(bo.getAppId() != null, WikiRecord::getAppId, bo.getAppId());
        if (bo.getAppId() != null) {
            lqw.eq(WikiRecord::getAppId, bo.getAppId());
        }else{
            lqw.isNull(WikiRecord::getAppId);
        }
        if (bo.getExpert() != null) {
            lqw.eq(WikiRecord::getExpert, "true");
        }
        return lqw;
    }

    /**
     * 新增AI提问记录
     *
     * @param bo AI提问记录
     * @return 是否新增成功
     */
    @Override
    public WikiRecord insertByBo(WikiRecordBo bo, String expert) {
        WikiRecord add = MapstructUtils.convert(bo, WikiRecord.class);
        validEntityBeforeSave(add);
        if (!StringUtils.isEmpty(expert)) {
            add.setExpert(expert);
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return add;
    }

    /**
     * 修改AI提问记录
     *
     * @param bo AI提问记录
     * @return 是否修改成功
     */
    @Override
    public WikiRecord updateByBo(WikiRecordBo bo) {
        WikiRecord update = MapstructUtils.convert(bo, WikiRecord.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        return update;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WikiRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除AI提问记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<WikiRecordVo> queryByAssistantId(ChatRecordQueryParam query) {
        LambdaQueryWrapper<WikiRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(query.getUserId() != null, WikiRecord::getUserId, query.getUserId());
        lqw.eq(query.getAssistantId() != null, WikiRecord::getAssistantId, query.getAssistantId());

        return baseMapper.selectVoList(lqw);
    }
}
